package com.fhminyi.dao;

import com.fhminyi.model.Order;
import com.fhminyi.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    // 获取所有订单
    public List<Order> getAllOrders() throws SQLException {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM 订单";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("订单_id"));
                order.setUserId(rs.getInt("用户_id"));
                order.setOrderDate(rs.getTimestamp("订单日期"));
                order.setTotalPrice(rs.getDouble("总价"));
                order.setStatus(rs.getString("状态"));
                orders.add(order);
            }
        }
        return orders;
    }

    // 根据订单ID获取订单
    public Order getOrderById(int orderId) throws SQLException {
        String sql = "SELECT * FROM 订单 WHERE 订单_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Order order = new Order();
                    order.setOrderId(rs.getInt("订单_id"));
                    order.setUserId(rs.getInt("用户_id"));
                    order.setOrderDate(rs.getTimestamp("订单日期"));
                    order.setTotalPrice(rs.getDouble("总价"));
                    order.setStatus(rs.getString("状态"));
                    return order;
                }
            }
        }
        return null;
    }

    // 根据用户ID获取订单列表
    public List<Order> getOrdersByUserId(int userId) throws SQLException {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM 订单 WHERE 用户_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Order order = new Order();
                    order.setOrderId(rs.getInt("订单_id"));
                    order.setUserId(rs.getInt("用户_id"));
                    order.setOrderDate(rs.getTimestamp("订单日期"));
                    order.setTotalPrice(rs.getDouble("总价"));
                    order.setStatus(rs.getString("状态"));
                    orders.add(order);
                }
            }
        }
        return orders;
    }

    // 添加订单
    public void addOrder(Order order) throws SQLException {
        String sql = "INSERT INTO 订单 (用户_id, 总价, 状态) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            pstmt.setInt(1, order.getUserId());
            pstmt.setDouble(2, order.getTotalPrice());
            pstmt.setString(3, order.getStatus());
            pstmt.executeUpdate();

            // 获取生成的订单ID
            try (ResultSet generatedKeys = pstmt.getGeneratedKeys()) {
                if (generatedKeys.next()) {
                    order.setOrderId(generatedKeys.getInt(1));
                } else {
                    throw new SQLException("Creating order failed, no ID obtained.");
                }
            }
        }
    }

    // 更新订单
    public void updateOrder(Order order) throws SQLException {
        String sql = "UPDATE 订单 SET 用户_id = ?, 总价 = ?, 状态 = ? WHERE 订单_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, order.getUserId());
            pstmt.setDouble(2, order.getTotalPrice());
            pstmt.setString(3, order.getStatus());
            pstmt.setInt(4, order.getOrderId());
            pstmt.executeUpdate();
        }
    }

    // 删除订单
    public void deleteOrder(int orderId) throws SQLException {
        String sql = "DELETE FROM 订单 WHERE 订单_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderId);
            pstmt.executeUpdate();
        }
    }
}